<?php
# require class Account
require_once "class/account.php";

/**
* Access of system users
* Authorize access of users of the system
* 
* @package Sign
* @see Account
* @author Argem Gerald R. Flores <argemgrflores@gmail.com>
* @version 1.0
*/
class Sign extends Account {
	/**
	* Sign constructor
	* Constructs mysql access
	* @param resource $mysqli
	*/
	function __construct($mysqli) {
		$this->mysqli = $mysqli; # assigns mysql access
	}
	
	/**
	* Sign in
	* 
	* Verifies credentials of user trying to sign in
	* Returns true if email and password is correct (registered)
	* Else, false if not
	* 
	* @access public
	* @param string $email
	* @param string $pword
	* @return boolean true on successful verification, false on not
	*/
	public function in($email, $pword) {
		# instantiate an Account for verification purposes
		$acct = new Account($this->mysqli, null);
		
		# verifies email and password
		$acct = $acct->verify($email, $pword);
		
		# incorrect email and/or password
		if(!$acct) {
			return false;
		}
		
		# assigns a cookie space for users id, email and type
		setcookie('acct[id]', $acct->ID, time()+(60*60*24*30));
		setcookie('acct[email]', $acct->Email, time()+(60*60*24*30));
		setcookie('acct[type]', $acct->Type, time()+(60*60*24*30));
		
		# verified credentials
		return true;
	} # function in
	
	/**
	* Sign out
	* 
	* Sets out a signed in user of the system
	* Removes cookie credentials and other user details
	* Returns true on successful signout, else false
	* 
	* @access public
	* @return boolean true on succcessful sign out, false on not
	*/
	public function out() {
		# Command to update last sign in date for user before signing out
		$upd = "
			UPDATE Accounts SET
				SgnOn='".date('Y-m-d H:i:s')."'
			WHERE ID='{$_COOKIE['acct']['id']}'
		";
		
		# Unsuccessful update of user
		if(!$this->mysqli->query($upd)) {
			return false;
		}
		
		# Removes cookie details of user's id, email and type
		setcookie('acct[id]', '', time()-60*60*24*30);
		setcookie('acct[email]', '', time()-60*60*24*30);
		setcookie('acct[type]', '', time()+(60*60*24*30));
		
		# Successful signout
		return true;
	} # function out
} # class Account

# Instantiate a new mysqli connection to MicroWebPH via the prime account
$mysqli = new mysqli("localhost", "prime", "pr1m34dm1n", "MicroWebPH");

# An error occurs, redirect to index page
if(mysqli_connect_errno()) {
	header('refresh: 0.1; url: http://localhost/microwebph/');
}

# Process sign in
if(isset($_GET["in"])) {
	# Instantiate a new Sign request for signing in
	$sign = new Sign($mysqli);
	
	# Verify sign in credentials
	$result = $sign->in($_POST["emailIn"], $_POST["pwordIn"]);
	
	# Unsuccesful, redirect to index page with error
	if(!$result) {
		header('refresh: 0.1; url="http://localhost/microwebph/?index&fail=in"'); return;
	}
	
	# Successful, redirect to home page
	header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view"');
} # get in
# Process sign out
elseif(isset($_GET["out"])) {
	# Instantiate a new Sign request for signing out
	$sign = new Sign($mysqli);
	
	# Verify sign out credentials
	$result = $sign->out();
	
	# Unsuccesful, redirect to index page with error
	if(!$result) {
		header('refresh: 0.1; url="http://localhost/microwebph/?index&fail=out"'); return;
	}
	
	# Successful, redirect to index page
	header('refresh: 0.1; url="http://localhost/microwebph/"');
} # Get out
# Process sign up
elseif(isset($_GET["up"])) {
	# Require class Application
	require_once("class/application.php");
	
	# Instantiate a new Application request for signing out
	$appn = new Application($mysqli, -1);
	
	# Create a new application
	$result = $appn->create($_POST["email"], $_POST["pword"], $_POST["lname"], $_POST["fname"], $_POST["mname"], $_POST["addrs"]);
	
	# Unsuccessful, redirect to index page with error
	if(!$result) {
		header('refresh: 0.1; url="http://localhost/microwebph?index&fail=up&em='.$_POST['email']); return;
	}
	
	# redirect to index page with success
	header('refresh: 0.1; url="http://localhost/microwebph?index&up='.$_POST["email"]); return;
} # get up

# close mysql connection
$mysqli->close();














?>
